/** @file hdf5_dataset.cc
 *
 *  @author Dongryeol Lee (drselee@gmail.com)
 */

#include <string>

#include "core/data/hdf5_dataset.h"
#include "hdf5.h"
#include "hdf5_hl.h"

namespace core {
namespace data {

Hdf5Dataset::~Hdf5Dataset() {
}

Hdf5Dataset::Hdf5Dataset() {
}

double Hdf5Dataset::average_num_points() const {
  return 0.0;
}

arma::vec &Hdf5Dataset::get_instance(int point_id) {
}

const arma::vec &Hdf5Dataset::get_instance(int point_id) const {
}

core::data::alias_vec &Hdf5Dataset::get_instance(
  int point_id, core::data::alias_vec **targets_out) {
}

const core::data::alias_vec &Hdf5Dataset::get_instance(
  int point_id, const core::data::alias_vec **targets_out) const {
}

void Hdf5Dataset::Init(int num_attributes, int num_points) {
}

void Hdf5Dataset::Init(
  boost::mpi::communicator &world,
  const std::string &file_name_in, bool from_single_file) {
}

int Hdf5Dataset::num_points() const {
  return 0;
}

void Hdf5Dataset::RandomGenerate(
  boost::mpi::communicator &world, int num_dimensions, int num_points)  {

}

bool Hdf5Dataset::Init(const std::string &file_name_in) {

  // For the moment, support only a two-dimensional dataset.
  std::string file_name_part =
    file_name_in.substr(0, file_name_in.find("/"));
  std::string dataset_name_part =
    file_name_in.substr(file_name_in.find("/") + 1);
  hid_t file_id =
    H5Fopen(file_name_part.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);

  int rank;
  H5LTget_dataset_ndims(file_id, dataset_name_part.c_str(), &rank);
  if(rank != 2) {
    std::cerr << "We support only two dimensional dataset...\n";
    return false;
  }
  std::vector<hsize_t> dims(2, 0);
  herr_t status =
    H5LTget_dataset_info(
      file_id, dataset_name_part.c_str(), dims.data(), NULL, NULL);

  // Read the dataset.
  matrix_.zeros(dims[0], dims[1]);
  H5LTread_dataset(
    file_id, dataset_name_part.c_str(), H5T_NATIVE_DOUBLE, matrix_.memptr());

  for(int j = 0; j < matrix_.n_cols; j++) {
    for(int i = 0; i < matrix_.n_rows; i++) {
      printf("%g ", matrix_.at(i, j));
    }
    printf("\n");
  }
}
}
}
